<html>
<head><meta charset="utf-8"><title>Portable SIMD Project Group help · project-const-generics · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/index.html">project-const-generics</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Portable.20SIMD.20Project.20Group.20help.html">Portable SIMD Project Group help</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="224816950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Portable%20SIMD%20Project%20Group%20help/near/224816950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Portable.20SIMD.20Project.20Group.20help.html#224816950">(Feb 02 2021 at 01:06)</a>:</h4>
<p>Hi all, I'm coming with a question from <a class="stream" data-stream-id="257879" href="/#narrow/stream/257879-project-portable-simd">#project-portable-simd</a> </p>
<p>We are using const generics for arbitrary width SIMD vectors (like <code>struct SimdF32&lt;const LANES: usize&gt;([f32; LANES]);</code>)</p>
<p>Unfortunately we're a little hung up on how to implement mask types.  We'd like some advice how to handle the following type (a follow-on to <a href="https://github.com/rust-lang/rust/issues/79778">https://github.com/rust-lang/rust/issues/79778</a> and <a href="https://internals.rust-lang.org/t/about-const-expressions-in-types-or-why-we-dont-need-const-evaluatable-checked/13602/17">https://internals.rust-lang.org/t/about-const-expressions-in-types-or-why-we-dont-need-const-evaluatable-checked/13602/17</a>, really):</p>
<div class="codehilite"><pre><span></span><code>#[repr(simd)]
struct Mask32&lt;const LANES: usize&gt;(
    // on some architectures, masks get 1 bit per lane
    #[cfg(target_arch = &quot;some_arch&quot;)] [u8; (LANES + 7) / 8],
    // on other architectures, masks get many bits per lane
    #[cfg(not(target_arch = &quot;some_arch&quot;)] [i32; LANES],
);

impl&lt;const LANES: usize&gt; Mask32 {
    // all architectures are convertible to 1 bit per lane
    fn to_bits(self) -&gt; [u8; (LANES + 7) / 8] { /* compiler intrinsic */ }
}
</code></pre></div>
<p>I think the important bit here is that <code>(LANES + 7) / 8</code> can be rewritten in a way that can't panic, so it seems a little unnecessary to expose bounds in the public API.  Additionally the bounds only apply to one of the two variants of the struct.  Any advice on how we should move forward with this?  Thanks!</p>
<p>cc <span class="user-mention" data-user-id="281757">@Jubilee</span></p>



<a name="224848621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Portable%20SIMD%20Project%20Group%20help/near/224848621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Portable.20SIMD.20Project.20Group.20help.html#224848621">(Feb 02 2021 at 09:51)</a>:</h4>
<p>While I really want a feature where we can avoid const-evaluatable-bounds for "division by known-non-zero" and other guaranteed non-panicking operations (like <code>(some_u8 as u16) + 7</code>), the current feature is already very experimental and not fully fleshed out. Starting to intermix random exceptions will not just be surprising to users but also open the question to "where do we stop". I don't think any of us currently have the capacity to design, implement or even "just" review such a scheme.</p>



<a name="224848846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Portable%20SIMD%20Project%20Group%20help/near/224848846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Portable.20SIMD.20Project.20Group.20help.html#224848846">(Feb 02 2021 at 09:53)</a>:</h4>
<p>It is entirely forward compatible to later remove bounds, so my personal preference would be that you start by exposing those bounds to the users (maybe wrapped in a const fn, so you don't expose the details. That const fn can then exist on all <code>target_arch</code> and just doesn't do anything on some of them). This may be annoying, but it wouldn't be the first time we start out with a weird limitation and lift it later (const fn comes to mind here).</p>



<a name="224909236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Portable%20SIMD%20Project%20Group%20help/near/224909236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Portable.20SIMD.20Project.20Group.20help.html#224909236">(Feb 02 2021 at 17:52)</a>:</h4>
<p>Hmm, I didn't think of that. If removing a bound really is ok then it probably is fine.</p>



<a name="224909278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Portable%20SIMD%20Project%20Group%20help/near/224909278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Portable.20SIMD.20Project.20Group.20help.html#224909278">(Feb 02 2021 at 17:52)</a>:</h4>
<p>(without thinking too hard it seems fine)</p>



<a name="224909951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Portable%20SIMD%20Project%20Group%20help/near/224909951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Portable.20SIMD.20Project.20Group.20help.html#224909951">(Feb 02 2021 at 17:57)</a>:</h4>
<p>I think I may also try using a type alias in the bound to hide the const fn entirely. Not sure if #[doc(hidden)] is appropriate in std but we'll see.  Thanks for the suggestion</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>